/**
 * Created by wujun on 2016/8/4
 */
angular.module('xyrhyddcx.controller',[])
  .controller('xyrhyddcxCtrl',['$scope','$state','xyrhyddcxService','$ionicPopover','$localstorage','$ionicPopup','$cordovaBarcodeScanner',function($scope,$state,xyrhyddcxService,$ionicPopover,$localstorage,$ionicPopup,$cordovaBarcodeScanner){
    $scope.params={};
    $scope.params=xyrhyddcxService.getParams();
    $scope.$on('$ionicView.enter', function(event, data) {

        if($localstorage.get("LOG_FLAG")=="1"){
          ddcxService.insertLog("订单查询").then(function(data){
            console.log("插入日志"+angular.toJson(data));
          },function(error){
            console.log("插入日志失败，请联系管理员");
          });
        }


        doQuery();

    });


    //下拉刷新
    $scope.doRefresh=function()
    {
      $scope.params.queryXyrhyddOrder.page=1;
      $scope.params.queryXyrhyddOrder.moreData=true;

      xyrhyddcxService.doRefresh($scope.params.queryXyrhyddOrder).then(function(data){

        if(data.respCode=="0")
        {
          angular.copy(data.data, $scope.params.xyrhyddOrderData);
          $scope.params.hasOrder="1";
        }
        else
        {
          $scope.params.xyrhyddOrderData=[]; //如果没有数据则清空数据源
          $scope.params.queryXyrhyddOrder.moreData=false;//上拉加载不可用
          $scope.params.hasOrder="0";
        }
      },function(error){
        //$scope.params.orderQueryParams.moreData=false;
        //$scope.params.hasOrder="0";
      });
      //要让scroller知道我们已经加载完了新项目，可以隐藏加载指示。为了达到这个目的，我们要广播scroll.refreshComplete事件
      $scope.$broadcast('scroll.refreshComplete');
    }
    //固网上拉加载
    $scope.doLoadMore=function() {
      $scope.params.queryXyrhyddOrder.page++;
      xyrhyddcxService.doRefresh( $scope.params.queryXyrhyddOrder).then(function(data){

        if(data.respCode=="0")
        {
          if(data.data.length==0|| $scope.params.queryXyrhyddOrder.page>data.data.totalPages) {
            $scope.params.queryXyrhyddOrder.moreData=false;
          } else {
            $scope.params.queryXyrhyddOrder.moreData=true;
            $scope.params.xyrhyddOrderData =$scope.params.xyrhyddOrderData.concat(data.data);
          }

          $scope.params.hasOrder="1";
        }
        else{
          $scope.params.queryXyrhyddOrder.moreData=false;
          $scope.params.hasOrder="0";
        }
      },function(error){
        $scope.params.queryXyrhyddOrder.moreData=false;
        $scope.params.hasOrder="0";
      }).finally(function() {
        // 停止广播ion-refresher
        $scope.$broadcast('scroll.infiniteScrollComplete');
      });
      //$scope.$broadcast('scroll.infiniteScrollComplete');
    }


    function doQuery(){
      //查询初始化
      $scope.params.queryXyrhyddOrder.page="1";
      $scope.params.queryXyrhyddOrder.moreData=true;

      xyrhyddcxService.doQueryXyrhyddOrderData($scope.params.queryXyrhyddOrder).then(function(data){

        if(data.respCode=="-1")//校验失败
        {
          var alertPopup=$ionicPopup.alert({
            title:'查询失败',
            template:data.respDesc,
            okText: '确定', // String (默认: 'OK')。OK按钮的文字。
            okType: 'item-customer-bg-darkred', // String (默认: 'button-positive')。OK按钮的类型。
          });
        }
        else //校验成功
        {
          angular.copy(data.data, $scope.params.xyrhyddOrderData);
        }
      },function(error){
      });
    }

  }]);
